/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jetradar.multibackstack.sample.slice;

import com.jetradar.multibackstack.BaseAbilitySlice;
import com.jetradar.multibackstack.bean.Action;
import com.jetradar.multibackstack.sample.ResourceTable;
import com.jetradar.multibackstack.sample.fraction.FavoritesFraction;
import com.jetradar.multibackstack.sample.fraction.ProFileFraction;
import com.jetradar.multibackstack.sample.fraction.SearchFraction;

import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

/**
 * MainAbilitySlice
 *
 * @since 2021-06-10
 */
public class MainAbilitySlice extends BaseAbilitySlice implements Component.ClickedListener {
    private static final int NUM_1 = -1;
    private static final int NUM_2 = 2;
    private static final int NUM_100 = 100;
    private static final float NUM_1F = 1.1f;
    private SearchFraction searchFraction;
    private FavoritesFraction favoritesFraction;
    private ProFileFraction proFileFraction;
    private Image searchImg;
    private Image favoritesImg;
    private Image profileImg;
    private Text searchText;
    private Text favoritesText;
    private Text profileText;
    private DirectionalLayout search;
    private DirectionalLayout favorites;
    private DirectionalLayout profile;
    private Color gray = new Color(Color.getIntColor("#CCCCCC"));
    private Color cyan = new Color(Color.getIntColor("#5AA035"));
    private Intent intents;
    private int selectInt = 0;

    /**
     * onStart
     *
     * @param intent intent
     */
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        this.intents = intent;
        super.setUIContent(ResourceTable.Layout_ability_main);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#6200EE"));
        searchImg = (Image) findComponentById(ResourceTable.Id_search_img);
        favoritesImg = (Image) findComponentById(ResourceTable.Id_favorites_img);
        profileImg = (Image) findComponentById(ResourceTable.Id_profile_img);
        searchText = (Text) findComponentById(ResourceTable.Id_search_text);
        favoritesText = (Text) findComponentById(ResourceTable.Id_favorites_text);
        profileText = (Text) findComponentById(ResourceTable.Id_profile_text);
        search = (DirectionalLayout) findComponentById(ResourceTable.Id_search);
        favorites = (DirectionalLayout) findComponentById(ResourceTable.Id_favorites);
        profile = (DirectionalLayout) findComponentById(ResourceTable.Id_profile);


        searchFraction = new SearchFraction(this);
        favoritesFraction = new FavoritesFraction(this);
        proFileFraction = new ProFileFraction(this);
        ((FractionAbility) getAbility()).getFractionManager()
                .startFractionScheduler()
                .add(ResourceTable.Id_show_stack, searchFraction)
                .add(ResourceTable.Id_show_stack, favoritesFraction)
                .add(ResourceTable.Id_show_stack, proFileFraction)
                .hide(favoritesFraction)
                .hide(proFileFraction)
                .show(searchFraction)
                .submit();
        addAction(searchFraction, NUM_1);
        search.setClickedListener(this::onClick);
        favorites.setClickedListener(this::onClick);
        profile.setClickedListener(this::onClick);
    }

    /**
     * setSelectInt
     *
     * @param seleIndex selectInt
     */
    public void setSelectInt(int seleIndex) {
        this.selectInt = seleIndex;
    }

    /**
     * onClick
     *
     * @param component component
     */
    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_search:
                searchClick();
                break;
            case ResourceTable.Id_favorites:
                favoritesClick();

                break;
            case ResourceTable.Id_profile:
                profileClick();

                break;
            case ResourceTable.Id_back:
                onBackPressed();
                initTitle();
                break;
            default:
                break;
        }

    }

    private void profileClick() {
        proFileFraction.showAll();
        if (selectInt != NUM_2) {
            initComponent();
            initTitle();
            setSearchAnimator(profile, profileImg, ResourceTable.Media_mine_true, profileText);
            ((FractionAbility) getAbility()).getFractionManager()
                    .startFractionScheduler()
                    .hide(searchFraction)
                    .hide(favoritesFraction)
                    .show(proFileFraction)
                    .submit();
            addAction(proFileFraction, NUM_1);
            selectInt = NUM_2;
        }
    }

    private void favoritesClick() {
        if (selectInt != 1) {
            initComponent();
            initTitle();
            setSearchAnimator(favorites, favoritesImg, ResourceTable.Media_heart_true, favoritesText);
            ((FractionAbility) getAbility()).getFractionManager()
                    .startFractionScheduler()
                    .hide(searchFraction)
                    .hide(proFileFraction)
                    .show(favoritesFraction)
                    .submit();
            favoritesFraction.showAll();
            addAction(favoritesFraction, NUM_1);
            selectInt = 1;
        }
    }

    private void searchClick() {
        if (selectInt != 0) {
            initComponent();
            initTitle();
            setSearchAnimator(search, searchImg, ResourceTable.Media_serch_true, searchText);
            ((FractionAbility) getAbility()).getFractionManager()
                    .startFractionScheduler()
                    .hide(favoritesFraction)
                    .hide(proFileFraction)
                    .show(searchFraction)
                    .submit();
            searchFraction.showAll();
            addAction(searchFraction, NUM_1);
            selectInt = 0;

        }
    }


    /***
     * initTitle
     */
    public void initTitle() {
    }

    private void setSearchAnimator(DirectionalLayout directionalLayout, Image searchimg, int p, Text text) {
        if (text.getTextColor().getValue() != cyan.getValue()) {
            directionalLayout.createAnimatorProperty().scaleX(NUM_1F).scaleY(NUM_1F).setDuration(NUM_100).start();
            searchimg.setPixelMap(p);
            text.setTextColor(cyan);
        }
    }

    private void initComponent() {
        searchImg.setPixelMap(ResourceTable.Media_search_false);
        favoritesImg.setPixelMap(ResourceTable.Media_heart_false);
        profileImg.setPixelMap(ResourceTable.Media_mine_false);
        searchText.setTextColor(gray);
        favoritesText.setTextColor(gray);
        profileText.setTextColor(gray);
        search.setScale(1, 1);
        favorites.setScale(1, 1);
        profile.setScale(1, 1);
    }

    /**
     * onBackPressed
     */
    @Override
    protected void onBackPressed() {
        if (list.size() == 1) {
            terminateAbility();
            return;
        }
        Action action = (Action) list.get(list.size() - NUM_2);
        int item = action.getItem();
        initComponent();
        if (action.getFraction() instanceof SearchFraction) {
            searchFraction.setItem(item);
            setSearchAnimator(search, searchImg, ResourceTable.Media_serch_true, searchText);
            selectInt = 0;
        } else if (action.getFraction() instanceof ProFileFraction) {
            proFileFraction.setItem(item);
            setSearchAnimator(profile, profileImg, ResourceTable.Media_mine_true, profileText);
            selectInt = NUM_2;
        } else if (action.getFraction() instanceof FavoritesFraction) {
            favoritesFraction.setItem(item);
            setSearchAnimator(favorites, favoritesImg, ResourceTable.Media_heart_true, favoritesText);
            selectInt = 1;
        }
        ((FractionAbility) getAbility()).getFractionManager()
                .startFractionScheduler()
                .hide(searchFraction)
                .hide(favoritesFraction)
                .hide(proFileFraction)
                .show(action.getFraction())
                .submit();
        if (list.size() != 0) {
            list.remove(list.size() - 1);
        }
    }
}